package com.mingle.iwalk;

import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.location.Location;

import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.Overlay;
import com.baidu.mapapi.Projection;

public class TrackPathOverlay extends Overlay {

	private List<Location> mLocList;
	private Paint mPaint;
	
	public TrackPathOverlay(List<Location> locList)
	{
		mPaint =  new Paint();
		mPaint.setColor(Color.BLUE);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(10);
		mPaint.setAlpha(90);
		mLocList = locList;
	}

	 private Point getPixelXYFromGeoValue( Projection p , double lat, double lng )
     {
         GeoPoint in = new GeoPoint((int) (lat * 1000000) , (int) (lng * 1000000) );
         Point out = new Point();
         p.toPixels(in, out);
         return out;
     }
	 
	@Override
	public void draw(Canvas canvas, MapView map, boolean shadow) {
				
		Projection p = map.getProjection();
        Point out;
        Path myPath = new Path(); 
         
        if(mLocList.size() > 1)
        {
	        for( int i=0; i<mLocList.size(); i++ )
	        {
	        	 Location loc = mLocList.get(i);
	             out = getPixelXYFromGeoValue( p, loc.getLatitude(),loc.getLongitude() );
	           
	        	 if( i == 0 )
	             {
	                 myPath.moveTo(out.x, out.y);
	             }
	             else if( i == mLocList.size() - 1)
	             {
	            	 myPath.setLastPoint(out.x, out.y);
	             }
	             else
	             {
	            	 myPath.lineTo(out.x, out.y);
	             }
	        }
	        
	        canvas.drawPath(myPath, mPaint); 
        }
        else if(mLocList.size() == 1)
        {
        	Location loc = mLocList.get(0);
            out = getPixelXYFromGeoValue( p, loc.getLatitude(),loc.getLongitude() );
          
        	canvas.drawPoint(out.x, out.y, mPaint);
        }
         
         super.draw(canvas, map, shadow);
	}
	
	
}
